var network = require('../utils/network_util.js');
var api = require('../utils/api.js');
var utils = require('../utils/utils.js');
let app = getApp();
// 点击bananer
function onBananerItemClick(target, e) {
  onItemClick(target, e);
}

// 点击按钮导航
function onNavigateItemClick(target, e) {
  onItemClick(target, e);
}

// 点击推荐商品
function onRecommendItemClick(target, e) {
  onItemClick(target, e);
}

// 点击拼团商品
function onSpellProductItemClick(target, e) {
  onItemClick(target, e);
}

// 点击文章
function onArticleItemClick(target, e) {
  onItemClick(target, e);
}

// 点击自适应图片
function onImageHItemClick(target, e) {
  onItemClick(target, e);
}

// 点击视频 
function onVideoItemClick(target, e) {
  onItemClick(target, e);
}

//点击领取优惠券
function clickhongbao(target, e) {
  var item = e.currentTarget.dataset.coupon;
  var couponId = [];
  for (var i = 0; i < item.length; i++) {
    couponId.push(item[i].id);
  }

  network._post_json(target, api.receiveCoupon, { couponId }, success);
  target.setData({
    isyouhuishow: true
  })
}

// 关闭弹屏
function powerDrawer(target, e) {

  target.setData({
    showModalStatus: false
  });
}

// 点击弹屏广告
function onAdvertItemClick(target, e) {
  target.setData({
    showModalStatus: false
  });
  onItemClick(target, e);
}

// 点击积分商品
function onintegralItemClick(target, e) {
  onItemClick(target, e);
}
/**
 * 所有商品 0 (旧数据) 
 * 商品详情 1 
 * 商品分类 2 
 * 优惠券列表 3 
 * 商品搜索 4 
 * 到店自提 5(旧数据) 
 * 拼团商品 6
 * 文章详情 7 
 * 文章分类 8 
 * 拼团列表 9
 * 积分商品列表 10 
 * 积分商品详情 11 
 * 分销页面 12 
 * 门店列表 13 
 * 门店详情 14
 * 
 * 分享返现 21
 * 会员开通 22
 * 个人中心 23
 * 我的订单 24
 * 我的优惠券 25
 * 我的拼团 26
 * 账户余额 27
 * 文章列表 28
 * 首页 29
 * 联系商家 30
 * 扫码买单 31
 * 位置导航 32
 */
// 点击Item
function onItemClick(target, e) {
  var item = e.currentTarget.dataset.item;
  if (item.linkType == 0) {
    if (item.linkId) {
      wx.navigateTo({
        url: '/pages/product/Product'
      })
    }
  } else if (item.linkType == 1) {
    // 店铺
    let store = {};
    store.id = target.data.store.id;
    store.name = target.data.store.name;
    store.imgUrl = target.data.store.img.imgUrl;
    store.district = target.data.store.district;
    store.address = target.data.store.address;
    if (item.linkId) {
      wx.navigateTo({
        url: '/pages/product/ProductDetails?productId=' + item.linkId + '&store=' + encodeURIComponent(JSON.stringify(store))
      })
    }
  } else if (item.linkType == 2) {
    if (item.linkId) {
      wx.setStorageSync("linkId", item.linkId);
      wx.switchTab({
        url: '/pages/product/Product'
      })
    }
  } else if (item.linkType == 3) {
    wx.navigateTo({
      url: '/pages/myself/coupon/TenantCoupon',
    })
  } else if (item.linkType == 4) {
    wx.navigateTo({
      url: '/pages/product/ProductSearch',
    })
  } else if (item.linkType == 5) {
    if (item.videoUrl) {
      wx.navigateTo({
        url: '/pages/home/VideoPlay?videoUrl=' + item.videoUrl,
      })
    }
  } else if (item.linkType == 6) {
    if (item.linkId) {
      wx.navigateTo({
        url: '/pages/product/SpellGroupDetails?spellGroupId=' + item.linkId
      })
    }
  } else if (item.linkType == 7) {
    if (item.linkId) {
      wx.navigateTo({
        url: '/pages/article/ArticleDetails?articleId=' + item.linkId,
      })
    }
  } else if (item.linkType == 8) {
    if (item.linkId) {
      wx.navigateTo({
        url: '/pages/article/Articles?linkId=' + item.linkId
      })
    }
  } else if (item.linkType == 9) {
    wx.navigateTo({
      url: '/pages/product/SpellGroupList'
    })
  } else if (item.linkType == 10) {
    wx.navigateTo({
      url: '/pages/myself/integral/Integral',
    })
  } else if (item.linkType == 11) {
    if (item.linkId) {
      wx.navigateTo({
        url: '/pages/myself/integral/RedeemDetails?productId=' + item.linkId
      })
    }
  } else if (item.linkType == 12) {
    wx.navigateTo({
      url: '/pages/myself/commission/Qrcode',
    })
  } else if (item.linkType == 13) {
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        let latitude = res.latitude;
        let longitude = res.longitude;
        wx.navigateTo({
          url: '/pages/myself/store/StoreInfo' + '?lng=' + longitude + '&lat=' + latitude + '&backTag=' + 0,
        })
      }
    })
    
  } else if (item.linkType == 14) {
    if (item.linkId) {
      wx.navigateTo({
        url: '/pages/myself/store/StoreDetails?linkId=' + item.linkId
      })
    }
  } else if (item.linkType == 21) {
    wx.navigateTo({
      url: '/pages/myself/commission/Qrcode'
    })
  } else if (item.linkType == 22) {
    wx.navigateTo({
      url: '/pages/myself/member/Member'
    })
    // if (app.globalData.userInfo.typeId) {
    //   wx.navigateTo({
    //     url: '/pages/myself/member/MyMember'
    //   })
    // } else {
      
    // }
  } else if (item.linkType == 23) {
    
  } else if (item.linkType == 24) {
    wx.navigateTo({
      url: '/pages/myself/order/Order?index=0'
    })
  } else if (item.linkType == 25) {
    wx.navigateTo({
      url: '/pages/myself/coupon/Coupon'
    })
  } else if (item.linkType == 26) {
    wx.navigateTo({
      url: '/pages/myself/myspellgroup/MySpellGroup?index=0'
    })
  } else if (item.linkType == 27) {
    wx.navigateTo({
      url: '/pages/myself/balance/Balance'
    })
  } else if (item.linkType == 28) {
    wx.navigateTo({
      url: '/pages/article/Articles'
    })
  } else if (item.linkType == 29) {
  } else if (item.linkType == 30) {
    let tel = app.globalData.store.phone;
    wx.makePhoneCall({
      phoneNumber: tel,
    })
  } else if (item.linkType == 31) {
    // wx.scanCode({
    //   onlyFromCamera: true,
    //   success: (res) => {
    //     var scene = decodeURIComponent(res.path);
    //     var index = scene.indexOf('scene=s=');
    //     // app.globalData.storeId == scene.substring(2, scene.length);
    //   }
    // })
    if (app.globalData.isFacePayment == 1){
      wx.navigateTo({
        url: '/pages/pay/FacePay'
      })
    }else{
      
    }
    

  } else if (item.linkType == 32) {
    wx.navigateTo({
      url: '/pages/myself/store/StoreDetails?store=' + encodeURIComponent(JSON.stringify(app.globalData.store))
    })
  }
}



module.exports = {
  onBananerItemClick: onBananerItemClick,
  onNavigateItemClick: onNavigateItemClick,
  onRecommendItemClick: onRecommendItemClick,
  onSpellProductItemClick: onSpellProductItemClick,
  onArticleItemClick: onArticleItemClick,
  powerDrawer: powerDrawer,
  onAdvertItemClick: onAdvertItemClick,
  onImageHItemClick: onImageHItemClick,
  onVideoItemClick: onVideoItemClick,
  // receiveCoupon: receiveCoupon,
  clickhongbao: clickhongbao,
  onintegralItemClick: onintegralItemClick
}